package test.copy;

class UserInfo implements Cloneable{
    int age;
    public UserInfo(int age) {
        this.age = age;
    }
    @Override
    protected UserInfo clone() throws CloneNotSupportedException {
        return (UserInfo)super.clone();
    }
}
class User implements Cloneable{
    String username;
    String pass;
    UserInfo userInfo;
    public User(String username, String pass, UserInfo userInfo) {
        this.username = username;
        this.pass = pass;
        this.userInfo = userInfo;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        User user = (User) super.clone();
        user.userInfo = this.userInfo.clone();
        return user;
    }
}
public class DeepCopy {

    public static void main(String[] args)throws  CloneNotSupportedException {
        User u1 = new User("张三", "123", new UserInfo(18));
        User u2 = (User) u1.clone();
        System.out.println(u1.userInfo.age == u2.userInfo.age);
        u1.userInfo.age=15;
        System.out.println(u1.userInfo.age == u2.userInfo.age);
    }


}
